//
//  HCStaredView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/20.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCStaredView.h"

#import "HCStardTableViewCell.h"

#import "HCStarDidModel.h"

@interface HCStaredView () <UITableViewDelegate,UITableViewDataSource> {
    UITableView *tableView_;
    HCStarDidModel *model_;
}

@end

@implementation HCStaredView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubView:frame];
    }
    return self;
}

-(void)initializeSubView:(CGRect)frame {
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView_.delegate= self;
    tableView_.dataSource = self;
    tableView_.showsVerticalScrollIndicator = NO;
    tableView_.rowHeight = 71;
    [self addSubview:tableView_];
    
    [tableView_ registerNib:[UINib nibWithNibName:@"HCStardTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"starcella"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (model_) {
        return model_.star.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HCStardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"starcella" forIndexPath:indexPath];
    
    HCStarDidData *data = model_.star[indexPath.row];
    cell.model = data;
    
    [cell.gotoButton addTarget:self action:@selector(cellBtnAction:event:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

-(void)cellBtnAction:(UIButton *)btn event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    
    CGPoint currentTouchPosition = [touch locationInView:tableView_];
    NSIndexPath *indexPath = [tableView_ indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"备用传值:%ld",indexPath.row);
    
    if ([self.delegate respondsToSelector:@selector(starViewClicked:)]&&self.delegate) {
        [self.delegate starViewClicked:indexPath];
    }
    
}

-(void)setModel:(id)model {
    model_ = (HCStarDidModel *)model;
    [tableView_ reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
